﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.Plumbing.ParamForm
{
	public partial class DataModifyForm : System.Windows.Forms.Form
	{
		public DataModifyForm(object selectObject)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.selectObject = selectObject;
		}

		private void DataModifyForm_Load(object sender, EventArgs e)
		{
			if (this.selectObject is ShareRPipeSystem)
			{
				this.Text = "共用立管数据批量修改";
			}
			else if (this.selectObject is FloorSystem)
			{
				this.Text = "楼层数据批量修改";
			}
			else if (this.selectObject is IndoorSystem)
			{
				this.Text = "户内数据批量修改";
				this.chkInDoorNum.Enabled = false;
				this.chkInDoorNum.Checked = false;
				this.hyTxtInDoorNum.Enabled = false;
			}
			else if (this.selectObject is CirclePipe)
			{
				this.Text = "环路数据批量修改";
				this.chkInDoorNum.Enabled = false;
				this.chkInDoorNum.Checked = false;
				this.chkCircleNum.Enabled = false;
				this.chkCircleNum.Checked = false;
				this.hyTxtInDoorNum.Enabled = false;
				this.hyTxtCircleNum.Enabled = false;
			}
			this.LoadData();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (!this.chkCircleNum.Checked && !this.chkHeatingPipe.Checked && !this.chkInDoorNum.Checked)
			{
				YJKMessageBox.Information("请选择要修改的对象。");
				return;
			}
			if ((this.hyTxtCircleNum.Text == "" && this.chkCircleNum.Checked) || (this.hyTxtInDoorNum.Text == "" && this.chkInDoorNum.Checked) || (this.hyTxtLoad.Text == "" && this.chkHeatingPipe.Checked) || (this.hyTxtPipeLength.Text == "" && this.chkHeatingPipe.Checked))
			{
				YJKMessageBox.Information("参数不能为空。");
				return;
			}
			if ((this.hyTxtCircleNum.Text == "0" && this.chkCircleNum.Checked) || (this.hyTxtInDoorNum.Text == "0" && this.chkInDoorNum.Checked) || (this.hyTxtLoad.Text == "0" && this.chkHeatingPipe.Checked) || (this.hyTxtPipeLength.Text == "0" && this.chkHeatingPipe.Checked))
			{
				YJKMessageBox.Information("参数不能为0。");
				return;
			}
			this.SaveData();
			base.DialogResult = DialogResult.OK;
		}

		private void LoadData()
		{
			foreach (double num in ConstantOfGHCalc.XPAPWJB)
			{
				this.cbxOutDiameter.Items.Add(num.ToString());
			}
			this.cbxOutDiameter.SelectedIndex = ConstantOfGHCalc.XPAPWJB.Count<double>() - 1;
			this.hyTxtInDoorNum.Text = "2";
			this.hyTxtCircleNum.Text = "2";
			this.hyTxtLoad.Text = "1000";
			this.hyTxtPipeLength.Text = "20";
		}

		private void SaveData()
		{
			if (this.chkInDoorNum.Checked)
			{
				this.ModifyIndoorNum();
			}
			if (this.chkCircleNum.Checked)
			{
				this.ModifyCircleNum();
			}
			if (this.chkHeatingPipe.Checked)
			{
				this.ModifyCircleData();
			}
		}

		private void ModifyIndoorNum()
		{
			if (this.selectObject is ShareRPipeSystem)
			{
				foreach (FloorSystem floorSystem in (this.selectObject as ShareRPipeSystem).FloorSystems)
				{
					this.UpdateIndoorSystem(floorSystem);
				}
			}
			if (this.selectObject is FloorSystem)
			{
				FloorSystem floorSystem2 = this.selectObject as FloorSystem;
				this.UpdateIndoorSystem(floorSystem2);
			}
		}

		private void ModifyCircleNum()
		{
			if (this.selectObject is ShareRPipeSystem)
			{
				foreach (FloorSystem floorSystem in (this.selectObject as ShareRPipeSystem).FloorSystems)
				{
					foreach (IndoorSystem indoorSystem in floorSystem.IndoorSystems)
					{
						this.UpdateCirclePipe(indoorSystem);
					}
				}
			}
			if (this.selectObject is FloorSystem)
			{
				foreach (IndoorSystem indoorSystem2 in (this.selectObject as FloorSystem).IndoorSystems)
				{
					this.UpdateCirclePipe(indoorSystem2);
				}
			}
			if (this.selectObject is IndoorSystem)
			{
				IndoorSystem indoorSystem3 = this.selectObject as IndoorSystem;
				this.UpdateCirclePipe(indoorSystem3);
			}
		}

		private void ModifyCircleData()
		{
			if (this.selectObject is ShareRPipeSystem)
			{
				IEnumerator<FloorSystem> enumerator = (this.selectObject as ShareRPipeSystem).FloorSystems.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						FloorSystem floorSystem = enumerator.Current;
						foreach (IndoorSystem indoorSystem in floorSystem.IndoorSystems)
						{
							foreach (CirclePipe circlePipe in indoorSystem.CirclePipes)
							{
								this.UpdateCircleData(circlePipe);
							}
						}
					}
					return;
				}
			}
			if (this.selectObject is FloorSystem)
			{
				IEnumerator<IndoorSystem> enumerator2 = (this.selectObject as FloorSystem).IndoorSystems.GetEnumerator();
				{
					while (enumerator2.MoveNext())
					{
						IndoorSystem indoorSystem2 = enumerator2.Current;
						foreach (CirclePipe circlePipe2 in indoorSystem2.CirclePipes)
						{
							this.UpdateCircleData(circlePipe2);
						}
					}
					return;
				}
			}
			if (this.selectObject is IndoorSystem)
			{
				IEnumerator<CirclePipe> enumerator3 = (this.selectObject as IndoorSystem).CirclePipes.GetEnumerator();
				{
					while (enumerator3.MoveNext())
					{
						CirclePipe circlePipe3 = enumerator3.Current;
						this.UpdateCircleData(circlePipe3);
					}
					return;
				}
			}
			if (this.selectObject is CirclePipe)
			{
				CirclePipe circlePipe4 = this.selectObject as CirclePipe;
				this.UpdateCircleData(circlePipe4);
			}
		}

		private void UpdateIndoorSystem(FloorSystem floorSystem)
		{
			int num = int.Parse(this.hyTxtInDoorNum.Text);
			int num2 = num - floorSystem.IndoorNum;
			if (num2 > 0)
			{
				for (int i = 0; i < num2; i++)
				{
					floorSystem.IndoorSystems.Add(new IndoorSystem(floorSystem.IndoorSystems[0]));
				}
				return;
			}
			for (int j = 0; j < -num2; j++)
			{
				floorSystem.IndoorSystems.RemoveAt(num);
			}
		}

		private void UpdateCirclePipe(IndoorSystem indoorSystem)
		{
			int num = int.Parse(this.hyTxtCircleNum.Text);
			int num2 = num - indoorSystem.CircleNum;
			if (num2 > 0)
			{
				for (int i = 0; i < num2; i++)
				{
					indoorSystem.CirclePipes.Add(new CirclePipe(indoorSystem.CirclePipes[0]));
				}
				return;
			}
			for (int j = 0; j < -num2; j++)
			{
				indoorSystem.CirclePipes.RemoveAt(num);
			}
		}

		private void UpdateCircleData(CirclePipe circlePipe)
		{
			circlePipe.Load = double.Parse(this.hyTxtLoad.Text);
			circlePipe.PipeData.Load = double.Parse(this.hyTxtLoad.Text);
			circlePipe.PipeData.Length = double.Parse(this.hyTxtPipeLength.Text);
			circlePipe.PipeData.OutDiameter = this.cbxOutDiameter.Text;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void chkInDoorNum_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkInDoorNum.Checked)
			{
				this.hyTxtInDoorNum.Enabled = true;
				return;
			}
			this.hyTxtInDoorNum.Enabled = false;
		}

		private void chkCircleNum_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkCircleNum.Checked)
			{
				this.hyTxtCircleNum.Enabled = true;
				return;
			}
			this.hyTxtCircleNum.Enabled = false;
		}

		private void chkHeatingPipe_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkHeatingPipe.Checked)
			{
				this.hyTxtLoad.Enabled = true;
				this.hyTxtPipeLength.Enabled = true;
				this.cbxOutDiameter.Enabled = true;
				return;
			}
			this.hyTxtLoad.Enabled = false;
			this.hyTxtPipeLength.Enabled = false;
			this.cbxOutDiameter.Enabled = false;
		}

		private object selectObject;
	}
}
